package com.zcq.aifitnessagent.tools;

import cn.hutool.core.io.FileUtil;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.zcq.aifitnessagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.io.IOException;

/**
 * pdf生成工具
 */
public class PDFGenerationTool {

    @Tool(description = "Generate a PDF file with given content", returnDirect = true)
    public String generatePDF(@ToolParam(description = "Name of the file to save the generated PDF") String fileName,
                              @ToolParam(description = "Content to be included in the PDF") String content) {
        String fileDir = FileConstant.FILE_SAVE_DIR + "/pdf";
        String filePath = fileDir + "/" + fileName;

        try {
            FileUtil.mkdir(fileDir);
            try (Document document = new Document(new PdfDocument(new PdfWriter(filePath)))) {
                PdfFont font = PdfFontFactory.createFont(
                        "STSong-Light", "UniGB-UCS2-H"
                );
                document.add(new Paragraph(content).setFont(font));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            return "PDF generated successfully to: " + filePath;
        } catch (RuntimeException e) {
            return "Error generating PDF: " + e.getMessage();
        }
    }

}
